package zust.se.delicious.action;

import java.util.List;
import java.util.Map;

import zust.se.delicious.service.BookmarkService;
import zust.se.delicious.service.UserService;
import zust.se.delicious.vo.BookmarkBean;
import zust.se.delicious.vo.PageBean;
import zust.se.delicious.vo.SessionUserBean;
import zust.se.delicious.vo.UserBean;

import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;

public class HomeAction extends ActionSupport {
	private int userId;
	private UserBean user;
	private String followAction;
	private String tabType;

	private BookmarkService bookmarkService;
	private UserService userService;

	private List<BookmarkBean> bmlist;
	private PageBean pageBean = new PageBean();

	public String executeList() {
		bmlist = bookmarkService.getList(userId, pageBean);
		user = userService.getHomeUser(userId);
		tabType = "listTab";

		Map session = ActionContext.getContext().getSession();
		SessionUserBean sessionUser = (SessionUserBean) session.get("sessionUser");
		int uid;
		if (sessionUser != null && ((uid = sessionUser.getId()) != userId)) {
			followAction = (!userService.isFollow(uid, userId)) ? "follow" : "unfollow";
		}
		return SUCCESS;
	}

	public String executeFeed() {
		bmlist = bookmarkService.getFeed(userId, pageBean);
		user = userService.getHomeUser(userId);
		tabType = "feedTab";
		Map session = ActionContext.getContext().getSession();
		SessionUserBean sessionUser = (SessionUserBean) session.get("sessionUser");
		int uid;
		if (sessionUser != null && ((uid = sessionUser.getId()) != userId)) {
			followAction = (!userService.isFollow(uid, userId)) ? "follow" : "unfollow";
		}
		return SUCCESS;
	}

	public String getTabType() {
		return tabType;
	}

	public void setTabType(String tabType) {
		this.tabType = tabType;
	}

	public String getFollowAction() {
		return followAction;
	}

	public void setFollowAction(String followAction) {
		this.followAction = followAction;
	}

	public UserBean getUser() {
		return user;
	}

	public void setUser(UserBean user) {
		this.user = user;
	}

	public PageBean getPageBean() {
		return pageBean;
	}

	public void setPageBean(PageBean pageBean) {
		this.pageBean = pageBean;
	}

	public void setUserService(UserService userService) {
		this.userService = userService;
	}

	public void setBookmarkService(BookmarkService bookmarkService) {
		this.bookmarkService = bookmarkService;
	}

	public int getUserId() {
		return userId;
	}

	public void setUserId(int userId) {
		this.userId = userId;
	}

	public List<BookmarkBean> getBmlist() {
		return bmlist;
	}

	public void setBmlist(List<BookmarkBean> bmlist) {
		this.bmlist = bmlist;
	}

}
